package com.haha.webSocket;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class BotHttpClient {

    private Bot bot;

    private HttpClient httpClient;

    public BotHttpClient(Bot bot) throws IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newHttpClient();
        this.httpClient = httpClient;
        this.bot = bot;
    }


    public HttpResponse<String> sendPost(String url, String body) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(bot.getBaseUrl() + url))
                .header("Content-Type", "application/json") // 设置请求头
                .header("Authorization", bot.getToken())
                .POST(HttpRequest.BodyPublishers.ofString(body))
                .build();
        return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
    }

    public HttpResponse<String> send(String url, String method, String body) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json") // 设置请求头
                .header("Authorization", bot.getToken())
                .method(method, HttpRequest.BodyPublishers.ofString(body))
                .build();
        return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
    }



}
